﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using FinalProject.Services.Interfaces;
using System.Web.Mvc;

namespace FinalProject.Services.Services
{
    public class CustomRoleProvider : RoleProvider, ICustomRoleProvider
    {
        private readonly IUserService _userService;

        public CustomRoleProvider()
            : this(DependencyResolver.Current.GetService<IUserService>())
        {
        }

        public CustomRoleProvider(IUserService userService)
        {
            this._userService = userService;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            var entity = _userService.GetUserWithRoles(username);
            var roles = new List<string>();
            foreach (var role in entity.UserRoles)
            {
                roles.Add(role.Name);
            }
            if (roles != null)
                return roles.ToArray<string>();
            return new string[] { };
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var entity = _userService.GetUserWithRoles(username);
            foreach (var role in entity.UserRoles)
            {
                if (role.Name == roleName)
                    return true;
            }
            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}
